//
//	  UserFileInterface.java
//
//	  Copyright (c) 2011 <John Lawniczak>
//	  jlawniczak340license@gmail.com
//
//    This file is part of CS102_Assignment2.
//
//    CS102_Assignment2 is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, version 3 of the License.
//
//    CS102_Assignment2 is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with CS102_Assignment2.  If not, see <http://www.gnu.org/licenses/>.
//
/**************************************************
Class Name:UserInterface
Author's Name:John Lawniczak
Date: 1.0	04-21-2010 for assignment 1
	  1.1	05-05-2010 for assignment 2
Description of the class: allows the user to 
	lookup and add lines to files and change files
***************************************/

import java.util.Scanner;


public class UserFileInterface {
	private File currFile;
	private int indexOfCurrFile =-1;
	private String nextCmd;
	private String [] params = new String[2];
	//private File file = null;
	private LinkedList <File>listOfFiles;
	private Scanner in = new Scanner(System.in);
	
	/**************************************************
	Constructor
	Input to the method: None
	Brief description of the task: starts the user 
		interface and creates a linked list of files
	Author: John Lawniczak
	**************************************************/	
	public UserFileInterface(){
		listOfFiles = new LinkedList<File>();
		runUI();
	}
	
	/**************************************************
	Method Name: runUI
	Input to the method: none
	Output(Return value): none
	Brief description of the task: runs the UI called 
		from the constructor
	Author: John Lawniczak
	**************************************************/
	private void runUI(){
		System.out.println("start");
		do
		{
			nextCmd = this.promptParams("enter a command to process (h for help, q to quit):");
			if(nextCmd.equals("h")){
				this.showHelpMessage();
			}else if(nextCmd.matches("[0-9]+")){
				this.displayLine(Integer.parseInt(nextCmd));
			}else if(nextCmd.equals("g")){
				this.findReplaceInCurrFile(promptParams("enter the string to find: "), promptParams("enter the string to replace: "));
			}else if(nextCmd.equals("a")){
				int index = this.promptIntParams("please enter the line number to add (-1 to cancel): ");
				if(index!=-1)
					this.addToFile(promptParams("enter the string to add: "), index);
				else
					System.out.println("did not add a line to the file");
			}else if(nextCmd.equals("r")){
				int index = this.promptIntParams("enter a line to remove (-1 to cancel): ");
				if(index != -1)
					this.removeLineNumber(index);
				else
					System.out.println("did not remove a line");
			}else if(nextCmd.equals("e")){
				int index = this.promptIntParams("enter a line to add a blank line to add (-1 to cancel): ");
				if(index != -1)
					this.addToFile("\r\n", index);
				else
					System.out.println("did not add an empty line");
			}else if(nextCmd.equals("wc")){
				System.out.println("number of lines: " + currFile.getNumLines());
				System.out.println("number of words: " + currFile.getNumWords());
				System.out.println("number of chars: " + currFile.getNumChars());
			}else if(nextCmd.equals("rp")){
				int index = this.promptIntParams("enter a line number to remove (-1 to cancel): ");
				if(index!=-1)
					this.replaceLine(this.promptParams("enter text to replace line with: "), index);
				else
					System.out.println("did not replace a line");
			}else if(nextCmd.equals("s")){
				this.currFile.saveFile();
			}else if(nextCmd.equals("sw")){
				String flnm = this.promptParams("enter a file name to switch to (leave blank to canel): ");
				if(!flnm.equals("")){
					this.saveCurrFile();
					this.switchCurrFile(flnm);
				}else{
					System.out.println("did not switch current file");
				}
			}else if(nextCmd.equals("o")){
				//System.out.println("open");
				String in = promptParams("enter file name: ");
				//System.out.println("open2");
				this.openFile(in);
				//System.out.println("open3");
			}else if(nextCmd.equals("q")){
				this.closeEditor();
				System.out.println("Good Bye");
			}else{
				System.out.println("sorry that was an invalid command\n");
				this.showHelpMessage();
			}
		}while(!nextCmd.equals("q"));
		
		this.closeEditor();
		in.close();
	}
	/**************************************************
	Method Name: openFile
	Input to the method: String filename, the name of the file to open
	Output(Return value): None
	Brief description of the task: opens the file given,
	 	creating a new file if necessary
	Author: John Lawniczak
	**************************************************/	
	public void openFile(String fileName){
		boolean found = false;
		for(int i =1; i<=listOfFiles.size();i++){
			if(listOfFiles.get(i).getFileName().equals(fileName)){
				currFile = listOfFiles.get(i);
				this.indexOfCurrFile = i;
				this.switchCurrFile(fileName);
				System.out.println(" file prexisting");
				System.out.println(this.currFile);
				return;
			}
		}
		java.io.File fle = new java.io.File(fileName);
		if(fle.exists()){
			found = true;
		}
		if(!found){
			listOfFiles.addLast(new File(fileName));
			currFile = listOfFiles.getLast();
			this.indexOfCurrFile = listOfFiles.size();
		}else{
			listOfFiles.addLast(new File(fileName));
			currFile = listOfFiles.getLast();
			this.indexOfCurrFile = listOfFiles.size();
			currFile.parseFile();
		}
		System.out.println("no file prexisting");
		System.out.print(currFile);
	}
	
	/**************************************************
	Method Name: saveCurrFile
	Input to the method: None
	Output(Return value): None
	Brief description of the task: Saves the file writing the lines to memory
	Author: John Lawniczak
	**************************************************/	
	public void saveCurrFile(){
		currFile.saveFile();
	}
	
	/**************************************************
	Method Name: closeCurrFile
	Input to the method: None
	Output(Return value): None
	Brief description of the task: Closes the current file 
		removing it from the list of files and setting the
		curr file to the first one added 
	Author: John Lawniczak
	**************************************************/	
	public void closeCurrFile(){
		//if()
		listOfFiles.remove(indexOfCurrFile);
		indexOfCurrFile--;
		currFile.closeFile();
		currFile = listOfFiles.getFirst();
	}
	
	/**************************************************
	Method Name: printFile
	Input to the method: None
	Output(Return value): None
	Brief description of the task: prints the contents 
		of the curr file to the screen
	Author: John Lawniczak
	**************************************************/	
	public void printCurrFile(){
		System.out.println(currFile);
	}
	
	/**************************************************
	Method Name: DisplayLine
	Input to the method: int the line to show
	Output(Return value): None
	Brief description of the task: prints the line to the screen
	Author: John Lawniczak
	**************************************************/	
	public void displayLine(int line){
		if(line <= this.currFile.getNumLines()){
			System.out.println(currFile.getFileLine(line).getText());
		}else{
			System.out.println("sorry you entered an invalid line number to view");
			System.out.println("there are " + this.currFile.getNumLines() + " lines");
		}
	}
	
	/**************************************************
	Method Name: findReplaceInCurrFile
	Input to the method: String the text to find, 
						 String the text to replace the found text with
	Output(Return value): String name
	Brief description of the task: performs a find/replace on the curren file
	Author: John Lawniczak
	**************************************************/	
	public void findReplaceInCurrFile(String find, String replace){
		currFile.findReplaceInFile(find, replace);
	}
	
	/**************************************************
	Method Name: findReplaceInCurrFile
	Input to the method: String the file to switch to
	Output(Return value): None
	Brief description of the task: switches the current 
		file creating a new one if there is none in the list of files
	Author: John Lawniczak
	**************************************************/	
	public void switchCurrFile(String fileName){
		closeCurrFile();
		openFile(fileName);
	}
	
	/**************************************************
	Method Name: addToFile
	Input to the method: String the text to add, 
						 int the line to add the text to
	Output(Return value): None
	Brief description of the task: adds the text to the line given of the current file
	Author: John Lawniczak
	**************************************************/	
	public void addToFile(String line, int index){
		currFile.addLine(line, index);
	}
	
	/**************************************************
	Method Name: addToFileEndLine
	Input to the method: String the text to add, 
	Output(Return value): None
	Brief description of the task: adds the text to the last line of the current file
	Author: John Lawniczak
	**************************************************/	
	public void addToFileEndLine(String line){
		currFile.addEndLine(line);
	}
	
	/**************************************************
	Method Name: addToFileFirstLine
	Input to the method: String the text to add 
	Output(Return value): None
	Brief description of the task: adds the text to the first line of the current file
	Author: John Lawniczak
	**************************************************/	
	public void addToFileFirstLine(String line){
		currFile.addFirstLine(line);
	}
	
	/**************************************************
	Method Name: removeLineNumber
	Input to the method: int line number to remove 
	Output(Return value): None
	Brief description of the task: removes the line number
	Author: John Lawniczak
	**************************************************/
	public void removeLineNumber(int i){
		currFile.removeLine(i);
	}
	
	/**************************************************
	Method Name: insertEmptyLine
	Input to the method: int line to insert empty line to 
	Output(Return value): None
	Brief description of the task: adds the text to the last line of the current file
	Author: John Lawniczak
	**************************************************/
	public void insertEmptyLine(int i){
		currFile.addEmptyLine(i);
	}
	
	public void replaceLine(String lne, int index){
		currFile.replaceLine(lne, index);
	}
	
	public void closeEditor(){
		for(int i =1; i<=listOfFiles.size();i++){
			listOfFiles.get(i).closeFile();
		}
	}
	
	
	/**************************************************
	Method Name: promptParams
	Input to the method: String message to show the user
	Output(Return value): string the user input
	Brief description of the task: prompts for user input
	 	and returns the input
	Author: John Lawniczak
	**************************************************/
	private String promptParams(String promptTxt) {
		System.out.println(promptTxt);
		return in.nextLine();
	}
	
	/**************************************************
	Method Name: promptIntParams
	Input to the method: String message to show the user
	Output(Return value): int the user input
	Brief description of the task: prompts for user input
	 	and returns the int input
	Author: John Lawniczak
	**************************************************/
	private int promptIntParams(String promptTxt) {
		int ret = -1;
		boolean correct = false;
		while(!correct){
			try{
				ret = Integer.parseInt(promptParams(promptTxt));
				correct = true;
			}catch (NumberFormatException e){
				correct = false;
				System.out.println("please enter a number: ");
			}
		}
		return ret;
	}
	/**************************************************
	Method Name:showhelpMessage 
	Input to the method: none
	Output(Return value): none
	Brief description of the task: shows the user all 
		valid commands to enter
	Author: John Lawniczak
	**************************************************/
	private void showHelpMessage(){
		System.out.println("This is the list of valid commands they are caps sensitive:");
		System.out.println("h  -\tshows this message");
		System.out.println("P  -\tprints all entries in data base");
		System.out.println("LT -\tlooks up and prints all entries with matching telephone number");
		System.out.println("LA -\tlooks up and prints all entries with matching address");
		System.out.println("LN -\tlooks up and prints all entries with matching name");
		System.out.println("A  -\tadds new entry to database");
		System.out.println("Q  -\tquits this program");
	}
	
	private void showHelpMessage(String msg){
		System.out.println(msg);
		System.out.println("This is the list of valid commands they are caps sensitive:");
		System.out.println("H  -\tshows this message");
		System.out.println("P  -\tprints all entries in data base");
		System.out.println("LT -\tlooks up and prints all entries with matching telephone number");
		System.out.println("LA -\tlooks up and prints all entries with matching address");
		System.out.println("LN -\tlooks up and prints all entries with matching name");
		System.out.println("A  -\tadds new entry to database");
		System.out.println("Q  -\tquits this program");
	}
	
	/**************************************************
	Method Name: cmdIsValid
	Input to the method: String command to validate
	Output(Return value): boolean
	Brief description of the task: checks to make sure 
		the string passed to it is a valid command
	Author: John Lawniczak
	**************************************************/
	private boolean cmdIsValid(String cmd){
		boolean validity = false;
		if(cmd.equals("n") || cmd.equals("a") || cmd.equals("h") || cmd.equals("e") || cmd.equals("rp") || cmd.equals("sw")|| cmd.equals("wc")|| cmd.equals("s")|| cmd.equals("q")){
			validity = true;
		}
		return validity;
	}
	
}
